package com.huawei.hianalytics.f.b;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import com.huawei.hianalytics.f.g.d;
import com.huawei.hianalytics.f.g.h;
import com.huawei.hianalytics.g.b;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class e extends c {
    private static final String j = e.class.getSimpleName();

    public static Map<String, Map<String, List<c>>> a(SharedPreferences sharedPreferences, Context context, String str, boolean z) {
        if (sharedPreferences == null || context == null) {
            return null;
        }
        Map<String, Map<String, List<c>>> hashMap = new HashMap();
        if (z) {
            Map b = h.b(sharedPreferences);
            if (b.size() > 200 || b.size() == 0) {
                b.c(j, "get state data ：The number of data obtained is too much! or No data : " + b.size());
                return hashMap;
            }
            for (Entry a : b.entrySet()) {
                a(a, context, hashMap);
            }
        } else {
            a(str, (String) h.b(sharedPreferences, str, ""), context, (Map) hashMap);
        }
        return hashMap;
    }

    private static void a(String str, String str2, Context context, Map<String, Map<String, List<c>>> map) {
        JSONArray jSONArray = null;
        try {
            jSONArray = !TextUtils.isEmpty(str2) ? new JSONArray(str2) : null;
        } catch (JSONException e) {
            b.c(j, "When events turn to JSONArray,JSON Exception has happened");
        }
        if (jSONArray != null && jSONArray.length() != 0) {
            Map hashMap = new HashMap();
            for (int i = 0; i < jSONArray.length(); i++) {
                try {
                    JSONObject jSONObject = jSONArray.getJSONObject(i);
                    c eVar = new e();
                    if (jSONObject != null) {
                        Object optString = jSONObject.optString("nc_common_flag");
                        eVar.b = jSONObject.optString("event", "");
                        eVar.c = d.a(jSONObject.optString("content"), context);
                        if (TextUtils.isEmpty(eVar.c)) {
                            b.c(j, "cache content is empty");
                        } else {
                            eVar.a = jSONObject.optString("eventtime");
                            eVar.e = jSONObject.optString("type");
                            eVar.i = optString;
                            if (jSONObject.has("event_session_name")) {
                                eVar.g = jSONObject.optString("event_session_name");
                                eVar.h = jSONObject.optString("first_session_event");
                            }
                            if (TextUtils.isEmpty(optString)) {
                                c.a(hashMap, eVar, "noExHashFlag");
                            } else {
                                c.a(hashMap, eVar, optString);
                            }
                        }
                    }
                } catch (JSONException e2) {
                    b.c(j, "readDataToRecord() events is not json format");
                }
            }
            map.put(str, hashMap);
        }
    }

    private static void a(Entry<String, String> entry, Context context, Map<String, Map<String, List<c>>> map) {
        a((String) entry.getKey(), (String) entry.getValue(), context, (Map) map);
    }

    public long a(Context context) {
        SharedPreferences d = h.d(context, "stat_v2_1");
        if (d == null || TextUtils.isEmpty(this.f)) {
            return -1;
        }
        JSONArray jSONArray;
        String str = this.f;
        String str2 = !"_default_config_tag".equals(this.f) ? str + "-" + this.e : str;
        try {
            jSONArray = new JSONArray((String) h.b(d, str2, ""));
        } catch (JSONException e) {
            jSONArray = new JSONArray();
        }
        try {
            JSONObject jSONObject = new JSONObject();
            jSONObject.put("event", this.b);
            jSONObject.put("content", this.c);
            jSONObject.put("eventtime", this.a);
            jSONObject.put("type", this.e);
            jSONObject.put("event_session_name", this.g);
            jSONObject.put("first_session_event", this.h);
            jSONObject.put("nc_common_flag", this.i);
            jSONArray.put(jSONObject);
            str = jSONArray.toString();
            Editor edit = d.edit();
            edit.remove(str2);
            edit.putString(str2, str);
            edit.apply();
            return (long) str.length();
        } catch (JSONException e2) {
            b.c(j, "When saveInSp() executed, JSON Exception has happened");
            return -1;
        }
    }
}
